// pages/student/studenthome/studenthome.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        home: true,
        my: false,
        headimg:'/pages/images/user.png',
        username:'学生',
        myusedata: [{
                id: 1,
                label: '个人信息',
                img: '/pages/images/teacher/geren.png'
            },
            {
                id: 2,
                label: '活动管理',
                img: '/pages/images/student/guanli.png'
            },
            {
                id: 3,
                label: '劳动总计',
                img: '/pages/images/student/zonji.png'
            },
            {
                id: 4,
                label: '设置中心',
                img: '/pages/images/shezhi.png'
            },

        ],
        current: 0,
        tabs: ['正在报名', '正在进行', '已结束'],
        index: null,
        tihuoWay: '校级',
        activity: [],
        dataTime:'',
        sdata:'',
        searchValue:'',
    },
    homeBar() {
        this.setData({
            home: true,
            my: false
        })
    },
    myBar() {
        this.setData({
            home: false,
            my: true
        })

    },
    bindShowMsg() {
        this.setData({
            select: !this.data.select
        })
    },
    tabSelect(e) {
        console.log(e.currentTarget.dataset.id)
        this.setData({
            current: e.currentTarget.dataset.id
        })
    },
    mySelect(e) {
        console.log(e)
        this.setData({
            tihuoWay: e.currentTarget.dataset.name,
            select: !this.data.select
        })
    },
    enterActivity(e) {
        var i = e.currentTarget.dataset.id
        var adata = JSON.stringify(this.data.activity[i])
        console.log(adata)
        wx.navigateTo({
            url: '/pages/student/studentactivity/studentactivity?adata=' + adata,
        })
    },
    searchCoach(e){
        console.log(e.detail.value)
        this.setData({
            searchValue:e.detail.value,
        })
    },
    async LoadData() {
        var sql = 'select * from activity'
        if (wx.getStorageSync('avatarUrl')) {
            this.setData({
                headimg: wx.getStorageSync('avatarUrl')
            })
        }
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        let dataTime
        let yy = new Date().getFullYear()
        let mm = new Date().getMonth() + 1
        let dd = new Date().getDate()
        if(mm<10){
            mm = '0'+mm
        }
        if(dd<10){
            dd = '0'+dd
        }
        dataTime = `${yy}-${mm}-${dd}`;
        console.log(dataTime)
        console.log(res1.data)
        this.setData({
            activity: res1.data,
            dataTime:dataTime,
        })
    },
    getHead(e) {
        var username = '微信用户'
        var headimg = e.detail.avatarUrl
        wx.setStorageSync('nickName', username)
        wx.setStorageSync('avatarUrl', headimg)
        this.setData({
            username: username,
            headimg: headimg
        })
    
    },
    async mysetBtn(e) {
        console.log(e.currentTarget.dataset.title)
        var title = e.currentTarget.dataset.title

        var url = ''
        var sql = 'select sno,sname,sd,sgrade,smajor,sphone from student where sno=' + this.data.sdata.user
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        console.log(res1.data[0])
        var signinfo = JSON.stringify(res1.data[0])
        var url = ''
        if (title === '个人信息') {
            url = '/pages/student/studentedit/studentedit?title=' + title + '&signinfo=' + signinfo
        } else if (title === '活动管理') {
            url = '/pages/student/studentmanage/studentmanage?title=' + title + '&signinfo=' + signinfo
        } else if (title === '劳动总计') {
            url = '/pages/student/studentlabor/studentlabor?title=' + title + '&signinfo=' + signinfo
        } else {
            url = '/pages/student/studentsite/studentsite?title=' + title + '&signinfo=' + signinfo
        }
        wx.navigateTo({
            url: url,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            sdata: wx.getStorageSync('student')
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        var username = wx.getStorageSync('nickName')
        var headimg = wx.getStorageSync('avatarUrl')
        if(username){
            this.setData({
                username:username,
                headimg:headimg
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.LoadData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})